<?php
/*******************************************************************************
 * Project: Bridge for WordPress to phpBB 3.0.x
 * File:    widget-posts.php
 * Author:  The Greek WordPress Comunity
 * Date:    Tuesday, 1st February 2011
 * -----------------------------------------------------------------------------
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

class phpBB3Posts extends WP_Widget
{
    function phpBB3Posts()
    {
        /* Widget settings. */
        $widget_ops = array(
            'classname' => 'phpBB3 Posts Widget',
            'description' => __('Allows you to display a list of recent topics within a specific forum id\'s.', 'wp_phpbb_bridge')
        );

        /* Widget control settings. */
        $control_ops = array(
            'width' => 250,
            'height' => 250,
            'id_base' => 'phpbb3-posts-widget'
        );

        /* Create the widget. */
        $this->WP_Widget(
            'phpbb3-posts-widget',
            'phpBB3 Posts Widget',
            $widget_ops,
            $control_ops
        );
    }

    function form($instance)
    {
        $defaults = array(
                        'title'=>'Latest posts',
        				'forums'=>0,
        				'total'=>'10',
                        'showForum'=>'yes',
                        'showUsername'=>'yes',
                        'showTotalPosts'=>'yes',
                        'showTotalViews'=>'yes'
                    );
            
        $instance = wp_parse_args(
                        (array)$instance,
                        $defaults
                    );
    ?>
    <div class="widget-content">
		<p>
            <label for="<?php echo $this->get_field_id('title'); ?>">
                <?php 
                    echo _e('Title:', 'wp_phpbb_bridge'); 
                ?>
            </label>
            <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $instance['title']; ?>" />
        </p>
		<p>
            <label for="<?php echo $this->get_field_id('forums'); ?>">
                <?php echo _e('Forums:', 'wp_phpbb_bridge'); ?>
            </label>
            <input name="<?php echo $this->get_field_name('forums'); ?>" type="text" id="<?php echo $this->get_field_id('forums'); ?>" value="<?php echo $instance['forums']; ?>" />
            <small><?php _e('Enter the id of the forum you like to get topics from. You can get topics from more than one forums by seperating the forums id with commas. ex: 3,5,6,12','wp_phpbb_bridge'); ?></small>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('total'); ?>">
                <?php 
                    echo _e('Total results:', 'wp_phpbb_bridge'); 
                ?>
            </label>
            <input name="<?php echo $this->get_field_name('total'); ?>" type="text" id="<?php echo $this->get_field_id('total'); ?>" value="<?php echo $instance['total']; ?>" />
        </p>
        <p> 
            <input name="<?php echo $this->get_field_name('showForum'); ?>" type="checkbox" id="<?php echo $this->get_field_id('showForum'); ?>" value="yes" <?php if ($instance['showForum'] == "yes") { echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('showForum'); ?>">
                <?php 
                    echo _e('Display forum name', 'wp_phpbb_bridge');
                ?>
            </label>
        </p>
        <p> 
            <input name="<?php echo $this->get_field_name('showUsername'); ?>" type="checkbox" id="<?php echo $this->get_field_id('showUsername'); ?>" value="yes" <?php if ($instance['showUsername'] == "yes") { echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('showUsername'); ?>">
                <?php 
                    echo _e('Display author name', 'wp_phpbb_bridge');
                ?>
            </label>
        </p>
        <p> 
            <input name="<?php echo $this->get_field_name('showTotalPosts'); ?>" type="checkbox" id="<?php echo $this->get_field_id('showTotalPosts'); ?>" value="yes" <?php if ($instance['showTotalPosts'] == "yes") { echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('showTotalPosts'); ?>">
                <?php 
                    echo _e('Display total replies', 'wp_phpbb_bridge');
                ?>
            </label>
        </p>
        <p> 
            <input name="<?php echo $this->get_field_name('showTotalViews'); ?>" type="checkbox" id="<?php echo $this->get_field_id('showTotalViews'); ?>" value="yes" <?php if ($instance['showTotalViews'] == "yes") { echo 'checked="checked" '; } ?> />
            <label for="<?php echo $this->get_field_id('showTotalViews'); ?>">
                <?php 
                    echo _e('Display total views', 'wp_phpbb_bridge');
                ?>
            </label>
        </p>
    </div>
    <?php
    }

    function update($new_instance, $old_instance)
    {
        $instance = $old_instance;

        $instance['title'] = $new_instance['title'];
        $instance['forums'] = $new_instance['forums'];
        $instance['total'] = $new_instance['total'];
        $instance['showForum'] = $new_instance['showForum'];
        $instance['showUsername'] = $new_instance['showUsername'];
        $instance['showTotalPosts'] = $new_instance['showTotalPosts'];
        $instance['showTotalViews'] = $new_instance['showTotalViews'];

        return $instance;
    }

    function widget($args, $instance)
    {
        extract($args);

        $title = $instance['title'];
        $forums = $instance['forums'];
        $total = $instance['total'];
        $showForum = $instance['showForum'];
        $showUsername = $instance['showUsername'];
        $showTotalPosts = $instance['showTotalPosts'];
        $showTotalViews = $instance['showTotalViews'];
        
        echo $before_widget . $before_title . $title . $after_title;
        wpb_get_latest_posts($forums, $total, $showForum, $showUsername, $showTotalPosts, $showTotalViews);
        echo $after_widget;
    }

}

function registerPostsWidget()
{
    register_widget('phpBB3Posts');
}

add_action('widgets_init', 'registerPostsWidget');

function wpb_get_latest_posts($cats = 0, $limit = 10, $showForum = 'yes', $showUserName = 'yes', $showTotalPosts = 'yes', $showTotalViews = 'yes')
{
    global $wpdb, $phpbb_root_path, $phpEx, $auth, $user, $db, $config, $cache, $template;

	$phpbb_config = trim(get_option('wpb_path'));
	$phpEx = substr(strrchr($phpbb_config, '.'), 1);

    $forum_url = str_replace("/ucp.php", "/", trim(get_option('wpb_url')));

	require($phpbb_config);

    $cn = mysql_pconnect($dbhost . ":" . $dbport, $dbuser, $dbpasswd);

    if($cn)
    {
        if(@mysql_select_db($dbname))
        {
            $qr = "SELECT p.`topic_id` AS `ID`, p2.`forum_name` AS `FORUM`, p.`forum_id` AS `FORUM_ID`, p.`topic_title` AS `TITLE`, p.`topic_views` AS `VIEWS`, p.`topic_replies` AS `REPLIES`, `username_clean` AS `USERNAME`, p.`topic_poster` AS `USER_ID`, p.`topic_time` AS `TIME`
FROM " . $table_prefix . "topics p
LEFT JOIN " . $table_prefix . "forums p2 ON(p.`forum_id` = p2.`forum_id`)
LEFT JOIN " . $table_prefix . "users p3 ON(p.`topic_poster` = p3.`user_id`)
WHERE p.`forum_id` IN (" . $cats . ") LIMIT " . $limit;

            $rs = @mysql_query($qr, $cn);
        }
    }

    ?>
    <ul>
    <?php
    while($get_info = mysql_fetch_assoc($rs))
    {
        ?>
        <li>
            <a href="<?php echo $forum_url; ?>viewtopic.php?f=<?php echo $get_info['FORUM_ID'] ?>&t=<?php echo $get_info['ID']; ?>" title="<?php echo __('View post','wp_phpbb_bridge'); ?>">
                <?php echo $get_info['TITLE']; ?>
            </a><br />
            <?php
                if($showForum == 'yes')
                {
            ?>
            <small>
                <?php 
                    echo __('Forum:', 'wp_phpbb_bridge');
                ?> 
                <a href="<?php echo $forum_url; ?>viewforum.php?f=<?php echo $get_info['FORUM_ID']; ?>" title="<?php echo __('Go to forum', 'wp_phpbb_bridge'); ?>">
                    <?php 
                        echo $get_info['FORUM']; 
                    ?>
                </a>
            </small>
            <br />
            <?php                    
                }
                
                if($showUserName == 'yes')
                {
            ?>
            <small>
                <?php 
                    echo __('By:', 'wp_phpbb_bridge'); 
                ?> 
                <a href="<?php echo $forum_url; ?>memberlist.php?mode=viewprofile&u=<?php echo $get_info['USER_ID']; ?>" title="<?php echo __('View user info', 'wp_phpbb_bridge'); ?>">
                    <?php 
                        echo $get_info['USERNAME']; 
                    ?>
                </a>
            </small>
            <br />
            <?php
                }
                
                if($showTotalViews == 'yes')
                {
            ?>
                <small>
                    <?php 
                        echo __('Views:', 'wp_phpbb_bridge'); 
                    ?> 
                    <strong>
                        <?php 
                            echo $get_info['VIEWS']; 
                        ?>
                    </strong>
                </small>
            <?php
                }
                
                if($showTotalViews == 'yes' && $showTotalPosts == 'yes')
                {
                    echo "<small>&nbsp;|&nbsp;</small>";
                }
                
                if($showTotalPosts == 'yes')
                {
            ?>
                <small>
                    <?php 
                        echo __('Replies:', 'wp_phpbb_bridge'); 
                    ?> 
                    <strong>
                        <?php 
                            echo $get_info['REPLIES']; 
                        ?>
                    </strong>
                </small>
            <?php
                }
            ?>
        </li>
        <?php
    }
    ?>
    </ul>
    <?php
}

?>
